package org.grapes.wine.api.server.method.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.grapes.wine.api.bean.MethodBean;
import org.grapes.wine.api.bean.ObjectFactory;
import org.grapes.wine.api.bean.SearchYachtBean;
import org.grapes.wine.api.method.EWineMethod;
import org.grapes.wine.api.server.method.handler.RenderDocument;
import org.grapes.wine.api.server.model.dao.YachtDAO;
import org.grapes.wine.api.server.model.search.HibernateSearcher;
import org.w3c.dom.Document;

public class SearchYacht extends RenderDocument {

	static Logger logger = Logger.getLogger(SearchYacht.class);
	private static List param = new ArrayList();
	static {
		param.add("match");
	}

	public Document execute(HttpServletRequest request) throws Exception {
		String match = request.getParameter("match");
		logger.info("match: " + match);

		List<YachtDAO> list = HibernateSearcher.search(YachtDAO.class, match);

		ObjectFactory factory = new ObjectFactory();
		SearchYachtBean bean = factory.createSearchYachtBean();

		if (list.size() > 0) {
			SearchYachtBean.SmallBean smallBean = null;
			for (YachtDAO yachtDAO : list) {
				smallBean = new SearchYachtBean.SmallBean();
				smallBean.setId(yachtDAO.getId());
				smallBean.setVersion(yachtDAO.getVersion());
				smallBean.setType(yachtDAO.getType());
				smallBean.setPrice(yachtDAO.getPrice());
				smallBean.setCapacity(yachtDAO.getCapacity());
				bean.getSmallBean().add(smallBean);
			}
			MethodBean methodBean = new MethodBean();
			methodBean.setBean(bean);
			methodBean.setClazz(SearchYachtBean.class);
			return initDocumentByJAXB(methodBean);
		}
		return null;
	}

	public EWineMethod getMethodName() {
		return EWineMethod.SEARCH_YACHT;
	}

	public List getParam() {
		return param;
	}
}
